/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web;

import com.imenu.ejb.ImenuCourseFacade;
import com.imenu.ejb.ImenuMenuFacade;
import com.imenu.ejb.ImenuTagFacade;
import com.imenu.entity.ImenuCourse;
import com.imenu.entity.ImenuMenu;
import com.imenu.entity.ImenuTag;
import com.imenu.entity.ImenuUser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "CourseServlet", urlPatterns = {"/course"})
public class CourseServlet extends BaseServlet<ImenuCourse> {

    void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String idParam = request.getParameter("id");
            Integer tagId = Integer.parseInt(request.getParameter("tagId"));
            ImenuTag tag = tagFacade.find(tagId);
            ImenuCourse course = null;
            if (idParam == null || idParam.length() < 1) {
                course = new ImenuCourse();
                this.parseRequest(request, course);
                course.setTag(tag);
                Integer menuId = Integer.valueOf(request.getParameter("menuId"));
                course.setMenu(menuFacade.find(menuId));
                course.setCreateTime(new java.util.Date(System.currentTimeMillis()));
                courseFacade.create(course);
            } else {
                Integer id = Integer.parseInt(idParam);
                course = courseFacade.find(id);
                this.parseRequest(request, course);
                course.setTag(tag);
                courseFacade.edit(course);
                System.out.println("CourseServlet.save() course = " + course);
                System.out.println("CourseServlet.save() course.getAvailable() = " + course.getAvailable());
                System.out.println("CourseServlet.save() course.getName() = " + course.getName());
                System.out.println("CourseServlet.save() course.getTagId() = " + course.getTag());
            }
            this.forward(request, response, "/course?command=courseList&menuId=" + course.getMenu().getId());
        } catch (Exception e) {
            this.error(request, response, e.getMessage());
        }
    }
    @EJB
    ImenuTagFacade tagFacade;

    void courseInfo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ImenuUser user = (ImenuUser) request.getSession().getAttribute("userInSession");
        List<ImenuTag> tagList = tagFacade.findByStoreId(user.getStore());
        ImenuCourse course = null;
        String courseIdParam = request.getParameter("courseId");
        if (courseIdParam != null && courseIdParam.length() > 0) {
            Integer courseId = Integer.valueOf(courseIdParam);
            course = courseFacade.find(courseId);
        } else {
            course = new ImenuCourse();
        }
        request.setAttribute("tagList", tagList);
        request.setAttribute("course", course);
        forward(request, response, "/course/courseInfo.jsp");
    }
    @EJB
    ImenuMenuFacade menuFacade;
    @EJB
    ImenuCourseFacade courseFacade;

    void courseList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ImenuUser user = (ImenuUser) request.getSession().getAttribute("userInSession");
        Integer menuId = Integer.valueOf(request.getParameter("menuId"));
        ImenuMenu menu = menuFacade.find(menuId);
        List<ImenuCourse> courseList = courseFacade.findByMenu(menu);;
        System.out.println("CourseServlet.courseList() menuList.size() = " + courseList.size());
        Map<ImenuTag, List<ImenuCourse>> courses = new HashMap<ImenuTag, List<ImenuCourse>>();
        for (ImenuCourse course : courseList) {
            if (courses.get(course.getTag()) == null) {
                courses.put(course.getTag(), new ArrayList<ImenuCourse>());
            }
            courses.get(course.getTag()).add(course);
            System.out.println("CourseServlet.courseList() menu.getImageLandscape() = " + course.getSmallLandscape());
            System.out.println("CourseServlet.courseList() menu.getSmallPortrait() = " + course.getSmallPortrait());
        }

        request.setAttribute("courses", courses);
        System.out.println("CourseServlet.courseList() user = " + user);
        forward(request, response, "/course/courseList.jsp");
    }

    void courseList1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MobileServlet.courseList() request.getParameter(menuName)=" + request.getParameter("menuName"));
        System.out.println("MobileServlet.courseList() request.getParameter(menuId)=" + request.getParameter("menuId"));
        Integer menuId = Integer.valueOf(request.getParameter("menuId"));
        ImenuMenu menu = menuFacade.find(menuId);
        TreeMap<ImenuTag, List<ImenuCourse>> courseByTag = new TreeMap<ImenuTag, List<ImenuCourse>>();
        for (ImenuCourse course : menu.getImenuCourseCollection()) {
            ImenuTag tag = course.getTag();
            if (courseByTag.get(tag) == null) {
                courseByTag.put(tag, new ArrayList<ImenuCourse>());
            }
            courseByTag.get(tag).add(course);
        }
        List<ImenuTag> tags = new ArrayList<ImenuTag>();
        List<List<ImenuCourse>> coursesLists = new ArrayList<List<ImenuCourse>>();
        for (ImenuTag tag : courseByTag.keySet()) {
            tags.add(tag);
            coursesLists.add(courseByTag.get(tag));
        }
        String tagIdParam = request.getParameter("tagId");
        ImenuTag preTag = null;
        ImenuTag curTag = null;
        ImenuTag nexTag = null;

        if (tagIdParam == null) {
            if (tags.size() > 0) {
                curTag = tags.get(0);
            }
            if (tags.size() > 1) {
                nexTag = tags.get(1);
            }
        } else {
            try {
                Integer tagId = Integer.valueOf(tagIdParam);
                for (int i = 0; i < tags.size(); i++) {
                    if (tags.get(i).getId().equals(tagId)) {
                        //System.out.println("bingo i="+i+" tags.size() = "+tags.size());
                        curTag = tags.get(i);
                        if (i > 0) {
                            preTag = tags.get(i - 1);
                        }
                        if ((tags.size() - i) > 1) {
                            nexTag = tags.get(i + 1);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<ImenuCourse> courses = courseByTag.get(curTag);
        //System.out.println("tagId="+tagIdParam);
        //System.out.println("preTag="+preTag);
        //System.out.println("curTag="+curTag);
        //System.out.println("nexTag="+nexTag);

        request.setAttribute("menuId", menuId);
        request.setAttribute("preTag", preTag);
        request.setAttribute("curTag", curTag);
        request.setAttribute("nexTag", nexTag);
        request.setAttribute("courses", courses);
        forward(request, response, "/course/" + request.getParameter("command") + ".jsp");
    }
}
